	
	package unitls;
	
	import java.sql.*;
	
	public class DataBaseConnection {
		private static final String dbdriver = "com.mysql.jdbc.Driver";
		private static final String dburl = "jdbc:mysql://localhost:3306/web_student";
		private static final String dbuser = "root";
		private static final String dbpassword = "1234";
	
		public static Connection getConnection() {
			Connection conn = null;
			try {
				Class.forName(dbdriver);// 加载jdbc驱动
				conn = DriverManager.getConnection(dburl, dbuser, dbpassword);// 链接数据库
			} catch (Exception e) {
				e.printStackTrace();
			}
			return conn;
		}
		public static Statement getStatement(Connection conn) throws ClassNotFoundException {
			Statement stmt = null; 
			try {
				if(conn != null) {
					stmt = conn.createStatement();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			return stmt;
		}
		

		public static ResultSet getResultSet(Statement stmt, String sql) {
			ResultSet rs = null;
			try {
				if(stmt != null) {
					rs = stmt.executeQuery(sql);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return rs;
		}
		
		public static void closeConn(Connection conn) {
			try {
				if(conn != null) {
					conn.close();
					conn = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		public static void closeStmt(Statement stmt) {
			try {
				if(stmt != null) {
					stmt.close();
					stmt = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		public static void closeRs(ResultSet rs) {
			try {
				if(rs != null) {
					rs.close();
					rs = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
